package com.aopcinemas.visitor;

import jade.content.lang.sl.SLCodec;
import jade.core.AID;
import jade.core.Agent;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import com.aopcinemas.cinema.model.Schedule;
import com.aopcinemas.cinema.ontology.CinemaOntology;
import com.aopcinemas.cinema.ontology.GetScheduleList;
import com.aopcinemas.common.model.Person;
import com.aopcinemas.visitor.model.Campaign;
import com.aopcinemas.visitor.ontology.CampaignRequest;
import com.aopcinemas.visitor.ontology.GetCampaign;
import com.aopcinemas.visitor.ontology.VisitorOntology;
import com.aopcinemas.visitor.ui.VisitorFrame;
import com.aopcinemas.visitor.ui.VisitorRegistrationDialog;

public class VisitorAgent extends Agent {

    private static final long serialVersionUID = 1L;

    private final SLCodec codec = new SLCodec();
    private List<AID> cinemas = new ArrayList<AID>();
    private List<String> friends = new ArrayList<String>();

    private Person person;

    private GetScheduleList getScheduleList;

    private VisitorFrame frame;

    private Campaign campaign;

    private GetCampaign getCampaign;

    public SLCodec getCodec() {
        return codec;
    }

    public Campaign getCampaign() {
        return campaign;
    }

    public GetCampaign getGetCampaign() {
        return getCampaign;
    }

    public void setGetCampaign(GetCampaign getCampaign) {
        this.getCampaign = getCampaign;
    }

    public void setCampaign(Campaign campaign) {
        this.campaign = campaign;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public List<AID> getCinemas() {
        return cinemas;
    }

    public void setCinemas(List<AID> cinemas) {
        this.cinemas = cinemas;
    }

    public GetScheduleList getGetScheduleList() {
        return getScheduleList;
    }

    public List<String> getFriends() {
        return friends;
    }

    public void setFriends(List<String> friends) {
        this.friends = friends;
    }

    public void setGetScheduleList(GetScheduleList getScheduleList) {
        this.getScheduleList = getScheduleList;
    }

    @Override
    protected void setup() {
        initializeOntology();
        initializeUI();
    }

    @Override
    protected void takeDown() {}

    private void initializeUI() {
        frame = new VisitorFrame(this);
        VisitorRegistrationDialog dialog = new VisitorRegistrationDialog(frame, this);
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
    }

    void initializeOntology() {
        // Register language and ontology
        getContentManager().registerLanguage(codec);
        getContentManager().registerOntology(CinemaOntology.getInstance());
        getContentManager().registerOntology(VisitorOntology.getInstance());
    }

    public void notifySchduleInfo(List<Schedule> list) {
        frame.listReceived(list);
    }

    public void notifyCampaign(GetCampaign campaign) {
        frame.campaignReceived(campaign);
    }

    public boolean campaignRequestReceived(CampaignRequest campaignRequest) {
        return frame.campaignRequestReceived(campaignRequest);
    }

    public void notifyOrderCampaign() {
        JOptionPane.showMessageDialog(frame, "All participants participate, order tickets!");
    }

    public void notifyMessage(String content) {
        frame.notifyMessage(content);
    }
}
